package com.nuc.workflowserver.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nuc.workflowserver.common.util.CommonUtils;
import com.nuc.workflowserver.mapper.IWorkMapper;
import com.nuc.workflowserver.model.dto.CheckWorkDTO;
import com.nuc.workflowserver.model.pojo.Work;
import com.nuc.workflowserver.model.vo.WorkVO;
import com.nuc.workflowserver.service.IWorkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class IWorkServiceImpl implements IWorkService {

    @Autowired
    IWorkMapper iWorkMapper;

    @Override
    public PageInfo<Work> selectPage(Work work, Integer pageNum,
            Integer pageSize) {
        String str = "yyy-MM-dd";
        SimpleDateFormat sdf = new SimpleDateFormat(str);
        PageHelper.startPage(pageNum, pageSize);
        List<Work> list = iWorkMapper.selectList(work);
        String strTemp = "";
        for (Work el : list) {
            strTemp = sdf.format(el.getWorkTime());
            el.setWorkTimeStr(strTemp);
        }
        PageInfo<Work> page = new PageInfo<>(list);
        return page;
    }

    @Override
    public List<Work> selectList(Work work) {
        return null;
    }

    @Override
    public Work selectByPrimary(Work work) {
        return null;
    }

    @Override
    public Integer update(Work work) {
        return iWorkMapper.update(work);
    }

    @Override
    public Integer delete(Work work) {
        return null;
    }

    @Override
    public Integer insert(Work work) {
        work.setId(CommonUtils.getUUID());
        work.setWorkStateId("1");
        return iWorkMapper.insert(work);
    }

    @Override
    public PageInfo<WorkVO> selectCheckWork(String userFlowId,
            CheckWorkDTO checkWorkDTO) {
        String str = "yyy-MM-dd";
        SimpleDateFormat sdf = new SimpleDateFormat(str);
        PageHelper.startPage(checkWorkDTO.getPageNum(),
                checkWorkDTO.getPageSize());
        Map<String, String> map = new HashMap<>();
        map.put("workStateId", checkWorkDTO.getWorkStateId());
        map.put("userFlowId", userFlowId);
        List<WorkVO> list = iWorkMapper.selectCheckWork(map);
        String strTemp = "";
        for (WorkVO el : list) {
            strTemp = sdf.format(el.getWorkTime());
            el.setWorkTimeStr(strTemp);
        }
        PageInfo<WorkVO> page = new PageInfo<>(list);
        return page;
    }

    @Override
    public List<Work> selectWorkForEcharts(String userId) {
        return iWorkMapper.selectListForEcharts(userId);
    }
}
